package string;

/**
 * @description: <a href="https://leetcode.cn/problems/compare-version-numbers"/>165. 比较版本号</a>
 * @author: zhaoyinglin
 * @date: 2024/3/28
 */
public class CompareVersionNumbers {

    public static void main(String[] args) {
        Solution solution = new Solution();

        System.out.println(solution.compareVersion("0.1", "1.1"));
    }

    static class Solution {
        public int compareVersion(String version1, String version2) {

            if (version1 == null || version2 == null) {
                return 0;
            }

            String[] v1s = version1.split("\\.");
            String[] v2s = version2.split("\\.");

            int v1Len = v1s.length, v2Len = v2s.length;
            int leftV1 = -1, leftV2 = -1;

            for (int i = 0; i < v1Len; i++) {
                if (i > v2Len - 1) {
                    leftV1 = i;
                    break;
                }

                if (i == v1Len - 1 && i < v2Len - 1) {
                    leftV2 = i + 1;
                }

                int compare = compareSingle(v1s[i], v2s[i]);
                if (compare != 0) {
                    return compare;
                }
            }

            while (leftV1 != -1 && leftV1 <= v1Len - 1) {
                if (!isZero(v1s[leftV1])) {
                    return 1;
                }
                leftV1++;
            }
            while (leftV2 != -1 && leftV2 <= v2Len - 1) {
                if (!isZero(v2s[leftV2])) {
                    return -1;
                }
                leftV2++;
            }
            return 0;
        }

        private int compareSingle(String s1, String s2) {
            char[] chs1 = s1.toCharArray(), chs2 = s2.toCharArray();

            int s1Start = -1, s2Start = -1;

            int s1Len = chs1.length, s2Len = chs2.length;

            for (int i = 0; i < chs1.length; i++) {
                if (chs1[i] == '0') {
                    continue;
                }
                s1Start = i;
                break;
            }

            for (int j = 0; j < chs2.length; j++) {
                if (chs2[j] == '0') {
                    continue;
                }
                s2Start = j;
                break;
            }

            if (s1Start == -1 || s2Start == -1) {
                return s1Start == -1 && s2Start == -1 ? 0 : s1Start == -1 ? -1 : 1;
            }

            if ((s1Len - s1Start) > (s2Len - s2Start)) {
                return 1;
            }

            if ((s1Len - s1Start) < (s2Len - s2Start)) {
                return -1;
            }

            for (int i = s1Start, j = s2Start; i < s1Len && j < s2Len; i++, j++) {
                Integer int1 = Integer.parseInt(String.valueOf(chs1[i]));

                Integer int2 = Integer.parseInt(String.valueOf(chs2[j]));

                if (int1.equals(int2)) {
                    continue;
                }
                if (int1 > int2) {
                    return 1;
                }
                return -1;
            }
            return 0;
        }

        private boolean isZero(String s) {
            char[] chs = s.toCharArray();

            int start = -1;

            for (int i = 0; i < chs.length; i++) {
                if (chs[i] == '0') {
                    continue;
                }
                start = i;
                break;
            }

            return start == -1;
        }

    }
}
